package com.hippo.core.tuya

import com.hippo.core.state.DeviceValue
import com.thingclips.smart.home.sdk.ThingHomeSdk

object DpCache {

    private val deviceDps = HashMap<String, HashMap<String,Any>>()

    fun updateDp(devId: String, data: Map<String, Any>) {
        val dps = getDps(devId)
        data.forEach { (t, u) ->
            dps[t] = u
        }
    }


    //从涂鸦重新获取最新dps
    fun refreshDps(devId: String): HashMap<String, Any>{
        val localDps = HashMap<String, Any>()
        val dps = ThingHomeSdk.getDataInstance().getDps(devId)
        dps?.forEach { (t, u) ->
            localDps[t] = u
        }
        deviceDps[devId] = localDps
        return localDps
    }




    fun getDps(devId: String): HashMap<String, Any> {
        return if (deviceDps.containsKey(devId)) {
            deviceDps[devId]!!
        }else{
            return refreshDps(devId)
        }
    }




    fun getDeviceValue(devId: String): DeviceValue {
        val dps = getDps(devId)
        return DeviceValue(
            switch = dps[DpType.SWITCH.dpId] as Boolean?,
            switch_go = dps[DpType.SWITCH_GO.dpId] as Boolean?,
            mode = dps[DpType.MODE.dpId] as String?,
            direction_control = dps[DpType.DIRECTION_CONTROL.dpId] as String?,
            status = dps[DpType.STATUS.dpId] as String?,
            battery_percentage = dps[DpType.BATTERY_PERCENTAGE.dpId] as Int?,
            suction = dps[DpType.SUCTION.dpId] as String?,
            clean_area = dps[DpType.CLEAN_AREA.dpId] as Int?,
            clean_time = dps[DpType.CLEAN_TIME.dpId] as Int?,
            water_quality_ph = dps[DpType.WATER_QUALITY_PH.dpId] as Int?,
            water_quality_salt = dps[DpType.WATER_QUALITY_SALT.dpId] as Int?,
            water_quality_oxygen = dps[DpType.WATER_QUALITY_OXYGEN.dpId] as Int?,
            water_quality_temperature = dps[DpType.WATER_QUALITY_TEMPERATURE.dpId] as Int?,
            water_quality_ec = dps[DpType.WATER_QUALITY_EC.dpId] as Int?,
            sensor_life = dps[DpType.SENSOR_LIFE.dpId] as Int?,
            slide_life = dps[DpType.SLIDE_LIFE.dpId] as Int?,
            appointment_notice = dps[DpType.APPOINTMENT_NOTICE.dpId] as Boolean?
        )
    }

}